GtkApplication: Add API to install accelerators for actions
authorMatthias Clasen <mclasen@redhat.com>
Mon, 5 Dec 2011 22:28:19 +0000 (17:28 -0500)
committerRyan Lortie <desrt@desrt.ca>
Mon, 19 Dec 2011 17:51:11 +0000 (12:51 -0500)
gtk/gtkapplication.c
gtk/gtkapplication.h

index 43e891be177e07e62abc00bb1ad0ac1bb056ef74..66cb48883c438289daefe60f85d5cebfee45fc14 100644 (file)
@@ -31,6 +31,7 @@
 #include "gtkmarshalers.h"
 #include "gtkmain.h"
 #include "gtkapplicationwindow.h"
+#include "gtkaccelmap.h"
 
 #include <gdk/gdk.h>
 #ifdef GDK_WINDOWING_X11
@@ -508,3 +509,109 @@ gtk_application_get_windows (GtkApplication *application)
 
   return application->priv->windows;
 }
+
+/* keep this in sync with gtkmodelmenuitem.c */
+static gchar *
+get_accel_path (const gchar *action_name,
+                GVariant    *parameter)
+{
+  GString *s;
+
+  s = g_string_new ("<Actions>/");
+  g_string_append (s, action_name);
+  if (parameter)
+    {
+      g_string_append_c (s, '/');
+      g_variant_print_string (parameter, s, FALSE);
+    }
+  return g_string_free (s, FALSE);
+}
+
+/**
+ * gtk_application_add_accelerator:
+ * @application: a #GtkApplication
+ * @accelerator: accelerator string
+ * @action_name: the name of the action to activate
+ * @parameter: (allow-none): parameter to pass when activating the action,
+ *   or %NULL if the action does not accept an activation parameter
+ *
+ * Installs an accelerator that will cause the named action
+ * to be activated when the key combination specificed by @accelerator
+ * is pressed.
+ *
+ * @accelerator must be a string that can be parsed by
+ * gtk_accelerator_parse(), e.g. "<Primary>q" or "<Control><Alt>p".
+ *
+ * @action_name must be the name of an action as it would be used
+ * in the app menu, i.e. actions that have been added to the application
+ * are referred to with an "app." prefix, and window-specific actions
+ * with a "win." prefix.
+ *
+ * Since: 3.4
+ */
+void
+gtk_application_add_accelerator (GtkApplication *application,
+                                 const gchar    *accelerator,
+                                 const gchar    *action_name,
+                                 GVariant       *parameter)
+{
+  gchar *accel_path;
+  guint accel_key;
+  GdkModifierType accel_mods;
+
+  g_return_if_fail (GTK_IS_APPLICATION (application));
+
+  /* Call this here, since gtk_init() is only getting called in startup() */
+  _gtk_accel_map_init ();
+
+  gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
+
+  if (accel_key == 0)
+    {
+      g_warning ("Failed to parse accelerator: '%s'\n", accelerator);
+      return;
+    }
+
+  accel_path = get_accel_path (action_name, parameter);
+
+  if (gtk_accel_map_lookup_entry (accel_path, NULL))
+    gtk_accel_map_change_entry (accel_path, accel_key, accel_mods, TRUE);
+  else
+    gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
+
+  g_free (accel_path);
+}
+
+/**
+ * gtk_application_remove_accelerator:
+ * @application: a #GtkApplication
+ * @action_name: the name of the action to activate
+ * @parameter: (allow-none): parameter to pass when activating the action,
+ *   or %NULL if the action does not accept an activation parameter
+ *
+ * Removes an accelerator that has been previously added
+ * with gtk_application_add_accelerator().
+ *
+ * Since: 3.4
+ */
+void
+gtk_application_remove_accelerator (GtkApplication *application,
+                                    const gchar    *action_name,
+                                    GVariant       *parameter)
+{
+  gchar *accel_path;
+
+  g_return_if_fail (GTK_IS_APPLICATION (application));
+
+  accel_path = get_accel_path (action_name, parameter);
+
+  if (!gtk_accel_map_lookup_entry (accel_path, NULL))
+    {
+      g_warning ("No accelerator found for '%s'\n", accel_path);
+      g_free (accel_path);
+      return;
+    }
+
+  gtk_accel_map_change_entry (accel_path, 0, 0, FALSE);
+  g_free (accel_path);
+}
index 785afac62584039ce9c0557711cd5732bd69e3ce..a75d66c41136daa360b6e0b221baeb448ed19409 100644 (file)
@@ -76,6 +76,13 @@ void             gtk_application_remove_window (GtkApplication    *application,
 
 GList *          gtk_application_get_windows   (GtkApplication    *application);
 
+void             gtk_application_add_accelerator    (GtkApplication  *application,
+                                                     const gchar     *accelerator,
+                                                     const gchar     *action_name,
+                                                     GVariant        *parameter);
+void             gtk_application_remove_accelerator (GtkApplication *application,
+                                                     const gchar    *action_name,
+                                                     GVariant       *parameter);
 
 G_END_DECLS